﻿var menuTitleElementSelector = ".menu-title";
var menuElementSelector = ".top-menu";

function AntiSpam(emailName, emailDomain) {
    var windowUrl = 'mailto:' + emailName + '@' + emailDomain;
    window.open(windowUrl);
}

function toggleBlocks() {

    $(".block").each(function () {
        var viewport = $.getSpikesViewPort();

        if (viewport.width >= 768) {
            $(this).children().eq(1).show();
        }
        else {
            $(this).children().eq(1).hide();
        }
    });
}

function SetDataTitle(columnNumber) {
    thVal = $('thead tr th').eq(columnNumber);
    $('tbody tr').each(function () {
        var tdVal = $(this).children("td")[columnNumber];
        $(tdVal).attr('data-title', thVal.text() + ":");
    });
}

function UpdateTables() {

    var childrenCount = $('thead tr th').length;
    for (var i = 0; i < childrenCount; i++) {
        SetDataTitle(i);
    }
}

function configureHeaderMenu() {
    var documentDimensions = $.getSpikesViewPort();

    if (((navigator.userAgent.indexOf('Firefox') != -1 || $.browser.msie) && documentDimensions.width <= 768) || (navigator.userAgent.indexOf('Firefox') == -1 && documentDimensions.width < 768)) {
        $(menuTitleElementSelector).show();
        $(menuElementSelector).hide();
    }
    else {
        $(menuTitleElementSelector).hide();
        $(menuElementSelector).show();
    }
}

$(document).ready(function () {
    var previousWidth = $.getSpikesViewPort().width;
    var checkWidth = function () {
        var viewport = $.getSpikesViewPort();
        if (viewport.width !== previousWidth) {
            previousWidth = viewport.width;

            toggleBlocks();

            configureHeaderMenu();
        }
    };

    window.addEventListener("resize", checkWidth, false);
    window.addEventListener("orientationchange", checkWidth, false);

    // (optional) Android doesn't always fire orientationChange on 180 degree turns
    setInterval(checkWidth, 2000);

    configureHeaderMenu();

    $(".block").each(function () {

        var that = this;

        $(this).find(".title strong").click(function () {

            var viewport = $.getSpikesViewPort();

            if (viewport.width < 768) {
                //$(that).find(".listbox").slideToggle("slow"); //instead of taking the .listbox we can take the second child of the .block element
                // as the blocks in the filters do not have the .listbox class, but filtersGroupPanel instead
                $(that).children().eq(1).slideToggle("slow");
            }
        });
    });

    $(menuTitleElementSelector).click(function () {
        if ($(menuElementSelector).is(":hidden")) {
            $(menuElementSelector).show();
        } else {
            $(menuElementSelector).hide();
        }
    });

    UpdateTables();
})